//该文件专门用于创建整个应用的路由器
import VueRouter from "vue-router";
//引入组件
import Login from "../components/Login";
import Home from "../components/Home";

//创建一个路由
const router = new VueRouter({
  routes: [
    {
      path: "/login",
      component: Login,
    },
    {
      //访问根路径时，自动重定向到登录页面
      path: "/",
      redirect: "/login",
    },
    {
      path: "/home",
      component: Home,
    },
  ],
});
//挂载一个路由导航守卫
router.beforeEach((to, from, next) => {
  //to 将要访问的路径
  //from 代表从哪个路径跳转而来
  //next 是一个函数，表示放行
  //  next() 放行  next('/login') 强制跳转
  if (to.path === "/login") return next();
  //获取token
  const tokenStr = window.sessionStorage.getItem("token");
  if(!tokenStr) return next('/login')
  next()
});

export default router;
